import time

import winsound
import cv2
import numpy as np

#import pyttsx3

def text_alert():
    # 模块初始化
    engine = pyttsx3.init()
    print('准备开始语音播报...')
    sound_alert()
    engine.say('警报，警报，警报，机房温度过高！')
    engine.say('警报，警报，警报，机房温度过高！')
    engine.say('警报，警报，警报，机房温度过高！')
    sound_alert()
    # 等待语音播报完毕
    engine.runAndWait()


def sound_alert():
    duration = 1000  # millisecond
    freq = 440  # Hz

    ## 高低频变化
    for i in range(30):
        winsound.Beep(freq, duration)
        freq = freq+100
        time.sleep(0.1)


# sound_alert()
# text_alert()
'''
【海康新版本，DS系列】
rtsp_url = rtsp

rtsp://username:password@<address>:<port>/Streaming/Channels/<id>(?parm1=value1&parm2-=value2…)
举例说明：

DS-9632N-ST的IP通道01主码流：

rtsp://admin:12345@172.6.22.234:554/Streaming/Channels/101?transportmode=unicast

DS-9016HF-ST的IP通道01主码流：

rtsp://admin:12345@172.6.22.106:554/Streaming/Channels/1701?transportmode=unicast

DS-9016HF-ST的模拟通道01子码流：

rtsp://admin:12345@172.6.22.106:554/Streaming/Channels/102?transportmode=unicast  (单播)

rtsp://admin:12345@172.6.22.106:554/Streaming/Channels/102?transportmode=multicast (多播)

rtsp://admin:12345@172.6.22.106:554/Streaming/Channels/102 (?后面可省略，默认单播)

DS-9016HF-ST的零通道主码流（零通道无子码流）：

rtsp://admin:12345@172.6.22.106:554/Streaming/Channels/001

DS-2DF7274-A的第三码流：

rtsp://admin:12345@172.6.10.11:554/Streaming/Channels/103
————————————————
版权声明：本文为CSDN博主「xiejiashu」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/xiejiashu/article/details/71786187
'''

#读取视频流  rtsp
# img1 = cv2.imread('Folder.jpg')


#定时读取图片
img1 = cv2.imread('pics/temp240.jpg', 0)

# cv2.imshow("img_source", img1)



# 手动定点 切割
dst_img = img1[180:280, 1310:1470]

cv2.imwrite('pingqie.jpeg',dst_img)

# 手动 仿射变换  拉直图像

pointSrc = [[25, 1], [159, 29], [9, 74], [143, 99]]

pointDest = [[0, 0], [136, 0], [0, 76], [136, 76]]

srcPoints = np.float32(pointSrc)
destPoints = np.float32(pointDest)


m = cv2.getPerspectiveTransform(srcPoints, destPoints)

resultImg = cv2.warpPerspective(dst_img, m, (139, 76), flags=cv2.INTER_AREA ,borderMode=cv2.BORDER_WRAP)


#手动测试30+度温度图片
#resultImg = cv2.imread('pics/ceshiyongli.jpg',0)


# 二值化 滤波去噪

# blur = cv2.GaussianBlur(resultImg,(5,5),0)
# cv2.imshow("blur_img", resultImg)

##

##二值化   0为二值化黑色  255为二值化白色
ret, thresh1 = cv2.threshold(resultImg,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow("img_binary", thresh1)

cv2.imwrite('resultImg.jpeg',thresh1)

# blur = cv2.GaussianBlur(resultImg,(3,3),0)
# cv2.imshow("img_blur", thresh1)

h, w = thresh1.shape

print(thresh1.shape)

print(thresh1.item(64,129))

# 列扫描法 竖切割首位数字
col_start = 999 #一个取不到的 超界限值
col_end = 999


for i in range(w//2):
    colum_tag = 0
    for j in range(h):
        if thresh1.item(j,i)==0:
            colum_tag = 1
            break
    if colum_tag == 0 and col_start == 999:
        col_start = i

    elif colum_tag == 0 and col_end == 999:
            for j in range(h):
                if thresh1.item(j, i-1) == 0:
                    col_end = i
                    break


print(col_start, col_end)

single_img = thresh1[:, col_start:col_end]
cv2.imshow("single_img", single_img)


# 列扫描法 横切割首位数字
## 从上之下

# single_img.shape[1]

row_start = 999
row_end = 999


for j in range(h//2):

    row_tag = 0
    for i in range(0,single_img.shape[1]-3):
        # if single_img.item(j, i) == 0 and single_img.item(j,i+3)==0 and single_img.item(j, i-3) == 0:
        if single_img.item(j, i) == 0 and single_img.item(j,i+1)==0 and single_img.item(j,i+2)==0  and single_img.item(j,i+3)==0:
            row_start = j
            break
    if row_start != 999:
        break
print('上边界{}'.format(j))



## 从下至上
for j in range(h-1,h//2,-1):
    # print(j)
    row_tag = 0
    for i in range(single_img.shape[1]-3):
        # print('\n')
        # print( j,i,single_img.item(j, i)) #超出边界
        # print('进入循环')
        if single_img.item(j, i) == 0 and single_img.item(j, i + 1) == 0 and single_img.item(j,i + 2) == 0 and single_img.item(j, i + 3) == 0:
            row_end = j
            break
    if row_end != 999:
        break
#
print('下边界{}'.format(j))


single_img = single_img[row_start:row_end, :]

cv2.imshow("single_img", single_img)




single_h ,single_w = single_img.shape


# print(single_h ,single_w )



##垂直中线 w w//2
col_midline = single_w//2
#水平中线 h = h//2

row_midline = single_h//2
## 垂直中线往下 四分之一

row_crossline =  row_midline + single_h//4


x_tag = 0

for i in range(single_w-3):
     if single_img.item(row_crossline,i)==0 and single_img.item(row_crossline,i+1)==0 and single_img.item(row_crossline,i+2)==0 and   single_img.item(row_crossline,i+3)==0:
        x_tag = i
        break

if x_tag <= col_midline:
    print('温度为{}0多度'.format(2))

if x_tag >= col_midline:
    print('温度为{}0多度'.format(3))




#上侧穿线

## 下侧穿线
# for i in range(0,w//2):
#     for j in range(h,h//2,-1):
#         if thresh1.item(i,j)==1 and thresh1(i,j+5)==1:
#             print(j,i)
#             break





cv2.waitKey(0)

cv2.destroyAllWindows()






## 膨胀运算
# kernel = np.ones((5,5),np.uint8)
#
# dilation = cv2.dilate(thresh1, kernel, iterations=1)
#
# cv2.imshow("dilation", dilation)
# cv2.waitKey(0)



## 切割首位数字




## 判断首位数字 是2 还是 3



## 直方图法















